package com.kx.yooli.kx.wechat.mapper.user;

import com.kx.yooli.kx.wechat.po.message.WechatValidateCode;
import com.kx.yooli.kx.wechat.po.user.UserBindAuthInfo;
import com.kx.yooli.kx.wechat.po.user.UserInfo;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.update.UpdateWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserInfoMapper extends BaseMapper<UserInfo> {
    @Select("SELECT * FROM user_info WHERE mobile = #{mobile}")
    UserInfo selectByPhone(@Param("mobile") String phone);

    @UseDataSource("m")
    @Select("SELECT ui.* FROM kxservicewechat.user_info ui where ui.mobile = #{mobile} ")
    UserInfo selectByPhoneAndCertNo(@Param("mobile") String phone);

    @Select("select count(id) from user_info where mobile = #{mobile,jdbcType=VARCHAR}")
    Integer selectCountByMobile(String mobile);
    @UseDataSource("m")
    default UserInfo selectCertNoByMobile(String mobile){
        QueryColumn serviceQuery = new QueryColumn("kxservicewechat","user_info","mobile");
        return  selectOneByCondition(QueryCondition.create(serviceQuery,"=",mobile));
    }
    @UseDataSource("ds4")
    default List<UserInfo> selectNplmCustByMobile(String mobile){
        QueryColumn column = new QueryColumn("nplm","NPLM_BASE_CUST_CONTACT_INFO","MOBILE_TEL");
        QueryWrapper queryWrapper = QueryWrapper.create().select(column);
        return selectListByQuery(queryWrapper);
    }

    default UserInfo selectByPhoneAndCertNoCopy(String phone){
        return selectOneWithRelationsByQuery(QueryWrapper.create()
                .select()
                .from(UserBindAuthInfo.class)
                        .where(
                                "mobile=" +selectOneWithRelationsByQuery(
                                        QueryWrapper.create().select("mobile").from("user_info").where("mobile="+ "'"+phone+"'")).getMobile()));
    }

    default UserInfo selectByCertNo(String idCard){
        return selectOneWithRelationsByQuery(QueryWrapper.create()
                .select()
                .from("user_info")
                .where("cert_no=" + "'"+idCard+"'"));
    }

    default UserInfo selectByPhoneDefault(String phone){
        return selectOneWithRelationsByQuery(QueryWrapper.create()
                .select()
                .from("user_info")
                .where("mobile=" + "'"+phone+"'"));
    }

    //根据身份证号修改手机号
    default boolean updateMobileByCertNo(UserInfo userInfo){
         return UpdateChain.of(UserInfo.class)
                .setRaw(UserInfo::getMobile,userInfo.getMobile())
                .where(UserInfo::getCertNo).eq(userInfo.getCertNo()).update();
    }


}
